package com.hmtech.aup.presentation.home

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.hmtech.aup.domain.vo.BaseResponse
import com.hmtech.aup.domain.vo.CategoryVO
import com.hmtech.aup.network.RetrofitClient
import kotlinx.coroutines.launch

class CategoryViewModel : ViewModel() {
    private val _categoriesResp = MutableLiveData<BaseResponse<List<CategoryVO>>>()

    // 公有只读 LiveData，外部只能 observe
    val categoriesResp: LiveData<BaseResponse<List<CategoryVO>>>
        get() = _categoriesResp

    fun loadAllWithStyles() {
        viewModelScope.launch {
            try {
                val resp = RetrofitClient.categoryApiService.listAllWithStyles()
                // 更新 LiveData
                _categoriesResp.postValue(resp)
            } catch (e: Exception) {
                Log.e("Network", "Error: ${e.message}")
            }
        }
    }
}